package com.ilxqx.framework.system.service.impl;

import com.ilxqx.framework.system.entity.SysMenu;
import com.ilxqx.framework.system.form.search.SysMenuSearchForm;
import com.ilxqx.framework.system.repository.SysMenuRepository;
import com.ilxqx.framework.system.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统菜单服务实现类
 * @author venus
 */
@Service
public class SysMenuServiceImpl extends AbstractSearchableServiceImpl<SysMenu, Long, SysMenuSearchForm> implements SysMenuService {

	private final SysMenuRepository sysMenuRepository;

	@Autowired
	public SysMenuServiceImpl(SysMenuRepository sysMenuRepository) {
		super(sysMenuRepository);
		this.sysMenuRepository = sysMenuRepository;
	}

	/**
	 * 根据ID判断记录是否存在
	 *
	 * @param menuId 记录ID
	 * @return bool
	 */
	@Override
	public boolean exists(Long menuId) {
		if (menuId.equals(0L)) {
			return true;
		}
		return super.exists(menuId);
	}

	/**
	 * 创建空的或者带有默认值的实体对象
	 *
	 * @return 实体对象
	 */
	@Override
	public SysMenu create() {
		SysMenu sysMenu = new SysMenu();
		sysMenu.setMeta(new SysMenu.Meta());
		return sysMenu;
	}

	/**
	 * 根据父级菜单主键获取子菜单
	 *
	 * @param parentMenuId 父级菜单主键
	 * @return 子菜单列表
	 */
	@Override
	public List<SysMenu> getListByParentMenuId(Long parentMenuId) {
		return this.sysMenuRepository.getAllByParentMenuIdOrderBySequenceAsc(parentMenuId);
	}

}
